clear all

use "$root\data\use.dta", replace
drop if yr==1999
capture erase "$root/results_replication/appendix_tables/tab_a6_insurance.xls"
capture erase "$root/results_replication/appendix_tables/tab_a6_insurance.txt"

gen corn_insured=corn_insured_acres/(corn_planted_fsa+corn_failed_fsa)
gen soybeans_insured=soybeans_insured_acres/(soybeans_planted_fsa+soybeans_failed_fsa)  //divide by planned acres

replace corn_insured=1 if corn_insured>1&~missing(corn_insured)
replace soybeans_insured=1 if soybeans_insured>1&~missing(soybeans_insured)
replace corn_weighted_cover=corn_weighted_cover*corn_insured
replace soybeans_weighted_cover=soybeans_weighted_cover*soybeans_insured


xtset fips year
* drop *38c*


foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00
**# OLS

**## Planned planting acres

	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_cover


**## Planting ratio
	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_weighted_cover  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_cover

	

**## Harvest ratio

	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_cover
	


**## Yield

	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_c
	
	

**## Production
	

	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_c
	
	* Output tables to see the numbers
	outreg2 [`crop'_pplanted] using "$root/results_replication/appendix_tables/tab_a6_insurance.xls", excel bdec(4) sdec(4) keep(d* *cover) nocon adjr2
	outreg2 [`crop'_plratio] using "$root/results_replication/appendix_tables/tab_a6_insurance.xls", excel bdec(4) sdec(4) keep(d* *cover) nocon adjr2
	outreg2 [`crop'_hratio] using "$root/results_replication/appendix_tables/tab_a6_insurance.xls", excel bdec(4) sdec(4) keep(d* *cover) nocon adjr2
	outreg2 [`crop'_yield] using "$root/results_replication/appendix_tables/tab_a6_insurance.xls", excel bdec(4) sdec(4) keep(d* *cover) nocon adjr2
	outreg2 [`crop'_production] using "$root/results_replication/appendix_tables/tab_a6_insurance.xls", excel bdec(4) sdec(4) keep(d* *cover) nocon adjr2

restore
}




